<?php
defined('IN_DESTOON') or exit('Access Denied');
require DT_ROOT.'/module/'.$module.'/common.inc.php';
$mid or $mid = 4;
$areaid = isset($areaid) ? intval($areaid) : 0;
if($mid > 4 && isset($MODULE[$mid]) && !$MODULE[$mid]['islink']) {
	$moduleid = $mid;
	$module = $MODULE[$mid]['module'];
	$modurl = $MODULE[$mid]['linkurl'];
	$table = get_table($mid);
	$rss_title = $MODULE[$mid]['name'];
	if($MOD['feed_enable']) {
		$pagesize = $MOD['feed_pagesize'] ? intval($MOD['feed_pagesize']) : 30;
		$condition = "status=3";
		if($MOD['feed_enable'] == 2) {
			if($kw) $rss_title = $rss_title.$DT['seo_delimiter'].$kw;
			if($keyword) $condition .= " and keyword LIKE '%$keyword%'";
			if($catid) {
				$condition .= $CAT['child'] ? " and catid IN (".$CAT['arrchildid'].")" : " and catid=$catid";
				$rss_title = $rss_title.$DT['seo_delimiter'].strip_tags(cat_pos($catid, $DT['seo_delimiter']));
			}
			if($areaid) {
				$condition .= $ARE['child'] ? " and areaid IN (".$ARE['arrchildid'].")" : " and areaid=$areaid";
				$rss_title = $rss_title.$DT['seo_delimiter'].strip_tags(area_pos($areaid, $DT['seo_delimiter']));
			}
		}
	}
	switch ($type){
        case 'json'://***@#$ 2018-10-22 17:15 by lmx | json是新闻频道原创给快传号提供的接口
            $content_table=get_table($mid,1);
            $modurl_mobile = $MODULE[$mid]['mobile'];
            header("Content-Type: text/plain");
            $list['data']=array();
            $ids=array();
            if($MOD['feed_enable']) {
                if($mid==44&$catid==242){//目前仅针对新闻原创
                    $result = $db->query("SELECT itemid,title,islink,linkurl,addtime FROM {$table} WHERE {$condition} ORDER BY addtime DESC LIMIT 0,$pagesize", 'CACHE');
                    while($r = $db->fetch_array($result)) {
                        $item=array();
                        $item['title']=$r['title'];
                        $item['content']="";
                        $item['publish_time']=timetodate($r['addtime'], 6);
                        $item['url']=!$r['islink']?($modurl.str_replace('&', '&amp;', $r['linkurl'])):$r['linkurl'];
                        $item['m_url']=!$r['islink']?$modurl_mobile.$r['linkurl']:$r['linkurl'];
                        $item['is_redirect']='1';
                        array_push($ids,$r['itemid']);
                        array_push($list['data'],$item);
                    }
                    $contents=$db->query("select content from {$content_table} where itemid in (".implode(',',$ids).") ORDER BY itemid DESC",'CACHE');
                    $i=0;
                    while($r = $db->fetch_array($contents)) {
                        $list['data'][$i]['content']=$r['content'];
                        $i++;
                    }
                }
                echo json_encode($list,JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
            }
            break;
        case 'xml'://***@#$ 2018-10-22 17:16 by lmx | 默认RSS接口均采用xml格式
        default:
            $rss_title = $rss_title.$DT['seo_delimiter'].$DT['sitename'];
            header("content-type:application/xml");
            echo '<?xml version="1.0" encoding="'.DT_CHARSET.'"?>';
            echo '<rss version="2.0">';
            echo '<channel>';
            echo '<title>'.$rss_title.'</title>';
            echo '<link>'.$modurl.'</link>';
            //	echo '<pubDate>'.timetodate($DT_TIME).'</pubDate>';
            if($MOD['feed_enable']) {
                $result = $db->query("SELECT itemid,title,introduce,copyfrom,islink,linkurl,addtime FROM {$table} WHERE {$condition} ORDER BY itemid DESC LIMIT 0,$pagesize", 'CACHE');
                //***@#$ 2018-11-20 16:22 by lmx | 针对商业原创添加全文输出
                if($mid==65&&$catid==700){
                    $content_table=get_table($mid,1);
                    $ids=array();$list=array();
                    while($r = $db->fetch_array($result)) {
                        array_push($ids,$r['itemid']);
                        array_push($list,$r);
                    }
                    $contents=$db->query("select content from {$content_table} where itemid in (".implode(',',$ids).") ORDER BY itemid DESC",'CACHE');
                    $i=0;
                    while($r = $db->fetch_array($contents)) {
                        $list[$i]['content']=$r['content'];
                        $i++;
                    }
                    foreach ($list as $item){
                        echo '<item>';
                        echo '<title><![CDATA['.$item['title'].']]></title>';
                        echo '<link>'.($item['islink']==1?$item['linkurl']:($modurl.str_replace('&', '&amp;', $item['linkurl']))).'</link>';
                        echo '<description><![CDATA['.($item['islink']==1?$item['introduce']:$item['content']).']]></description>';
                        echo '<source><![CDATA['.($item['copyfrom']==''?'映象网':$item['copyfrom']).']]></source>';
                        echo '<pubDate>'.timetodate($item['addtime'], 6).'</pubDate>';
                        echo '</item>';
                    }
                }else{
                    while($r = $db->fetch_array($result)) {
                        echo '<item>';
                        echo '<title><![CDATA['.$r['title'].']]></title>';
                        //echo '<link>'.$modurl.str_replace('&', '&amp;', $r['linkurl']).'</link>';
                        echo '<link>'.($r['islink']==1?$r['linkurl']:($modurl.str_replace('&', '&amp;', $r['linkurl']))).'</link>';
                        echo '<description><![CDATA['.$r['introduce'].']]></description>';
                        echo '<source><![CDATA['.($r['copyfrom']==''?'映象网':$r['copyfrom']).']]></source>';
                        echo '<pubDate>'.timetodate($r['addtime'], 6).'</pubDate>';
                        echo '</item>';
                    }
                }
            } else {
                echo '<item id="0">';
                echo '<title><![CDATA['.$L['rss_close'].']]></title>';
                echo '<link>'.DT_PATH.'</link>';
                echo '<description><![CDATA['.$L['rss_close'].']]></description>';
                echo '<pubDate>'.timetodate($DT_TIME, 6).'</pubDate>';
                echo '</item>';
            }
            echo '</channel>';
            echo '</rss>';
            break;
    }

} else {
	dheader('./');
}
?>